package org.eiahe.hr.salary.controller;

import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.core.validate.QueryGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.salary.domain.bo.SalaryNotificationBo;
import org.eiahe.hr.salary.domain.vo.SalaryNotificationVo;
import org.eiahe.hr.salary.service.ISalaryNotificationService;
import org.ehe.common.excel.utils.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 薪资通知Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/salary/notification")
public class SalaryNotificationController extends BaseController {

    private final ISalaryNotificationService iSalaryNotificationService;

    /**
     * 查询薪资通知列表
     */
    @SaCheckPermission("salary:notification:list")
    @GetMapping("/list")
    public TableDataInfo<SalaryNotificationVo> list(SalaryNotificationBo bo, PageQuery pageQuery) {
        return iSalaryNotificationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出薪资通知列表
     */
    @SaCheckPermission("salary:notification:export")
    @Log(title = "薪资通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SalaryNotificationBo bo, HttpServletResponse response) {
        List<SalaryNotificationVo> list = iSalaryNotificationService.queryList(bo);
        ExcelUtil.exportExcel(list, "薪资通知数据", SalaryNotificationVo.class, response);
    }

    /**
     * 获取薪资通知详细信息
     *
     * @param notificationId 主键
     */
    @SaCheckPermission("salary:notification:query")
    @GetMapping("/{notificationId}")
    public R<SalaryNotificationVo> getInfo(
            @NotNull(message = "主键不能为空") @PathVariable("notificationId") Long notificationId) {
        return R.ok(iSalaryNotificationService.queryById(notificationId));
    }

    /**
     * 新增薪资通知
     */
    @SaCheckPermission("salary:notification:add")
    @Log(title = "薪资通知", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SalaryNotificationBo bo) {
        return toAjax(iSalaryNotificationService.insertByBo(bo));
    }

    /**
     * 修改薪资通知
     */
    @SaCheckPermission("salary:notification:edit")
    @Log(title = "薪资通知", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SalaryNotificationBo bo) {
        return toAjax(iSalaryNotificationService.updateByBo(bo));
    }

    /**
     * 删除薪资通知
     *
     * @param notificationIds 主键串
     */
    @SaCheckPermission("salary:notification:remove")
    @Log(title = "薪资通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{notificationIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] notificationIds) {
        return toAjax(iSalaryNotificationService.deleteWithValidByIds(Arrays.asList(notificationIds)));
    }

    /**
     * 生成薪资通知
     */
    @SaCheckPermission("salary:notification:generate")
    @Log(title = "薪资通知", businessType = BusinessType.INSERT)
    @PostMapping("/generate/{yearMonth}")
    public R<Void> generate(@PathVariable("yearMonth") String yearMonth) {
        return toAjax(iSalaryNotificationService.generateNotifications(yearMonth));
    }

    /**
     * 发送薪资通知
     */
    @SaCheckPermission("salary:notification:send")
    @Log(title = "薪资通知", businessType = BusinessType.UPDATE)
    @PostMapping("/send/{notificationId}")
    public R<Void> send(@NotNull(message = "通知ID不能为空") @PathVariable Long notificationId) {
        return toAjax(iSalaryNotificationService.sendNotification(notificationId));
    }

    /**
     * 标记通知为已查看
     */
    @SaCheckPermission("salary:notification:view")
    @Log(title = "薪资通知", businessType = BusinessType.UPDATE)
    @PostMapping("/view/{notificationId}")
    public R<Void> view(@NotNull(message = "通知ID不能为空") @PathVariable Long notificationId) {
        return toAjax(iSalaryNotificationService.markAsViewed(notificationId));
    }
}
